package com.example.xncs;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

/**
 * 图片数据管理器，用于处理大量base64数据的存储和读取
 */
public class ImageDataManager {
    private static final String TAG = "ImageDataManager";
    private static final String PREFS_NAME = "image_data_prefs";
    private static final String KEY_BASE64_COUNT = "base64_count";
    private static final String KEY_IMAGE_NAME_COUNT = "image_name_count";
    private static final String KEY_BASE64_PREFIX = "base64_";
    private static final String KEY_IMAGE_NAME_PREFIX = "image_name_";
    
    private Context context;
    private SharedPreferences prefs;
    
    public ImageDataManager(Context context) {
        this.context = context;
        this.prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
    }
    
    /**
     * 保存base64图片数据列表
     */
    public void saveBase64ImageList(List<String> base64List, List<String> imageNames) {
        try {
            SharedPreferences.Editor editor = prefs.edit();
            
            // 清除旧数据
            clearAllData();
            
            // 保存base64数据
            editor.putInt(KEY_BASE64_COUNT, base64List.size());
            for (int i = 0; i < base64List.size(); i++) {
                String key = KEY_BASE64_PREFIX + i;
                String value = base64List.get(i);
                
                // 如果base64数据太大，截断并记录警告
                if (value != null && value.length() > 100000) { // 100KB限制
                    Log.w(TAG, "Base64 data too large for position " + i + ", truncating");
                    value = value.substring(0, 100000);
                }
                
                editor.putString(key, value);
            }
            
            // 保存图片名称
            editor.putInt(KEY_IMAGE_NAME_COUNT, imageNames.size());
            for (int i = 0; i < imageNames.size(); i++) {
                String key = KEY_IMAGE_NAME_PREFIX + i;
                editor.putString(key, imageNames.get(i));
            }
            
            editor.apply();
            Log.d(TAG, "Saved " + base64List.size() + " base64 images and " + imageNames.size() + " names");
            
        } catch (Exception e) {
            Log.e(TAG, "Error saving base64 image list", e);
        }
    }
    
    /**
     * 读取base64图片数据列表
     */
    public List<String> loadBase64ImageList() {
        List<String> base64List = new ArrayList<>();
        try {
            int count = prefs.getInt(KEY_BASE64_COUNT, 0);
            for (int i = 0; i < count; i++) {
                String key = KEY_BASE64_PREFIX + i;
                String value = prefs.getString(key, "");
                base64List.add(value);
            }
            Log.d(TAG, "Loaded " + base64List.size() + " base64 images");
        } catch (Exception e) {
            Log.e(TAG, "Error loading base64 image list", e);
        }
        return base64List;
    }
    
    /**
     * 读取图片名称列表
     */
    public List<String> loadImageNamesList() {
        List<String> imageNames = new ArrayList<>();
        try {
            int count = prefs.getInt(KEY_IMAGE_NAME_COUNT, 0);
            for (int i = 0; i < count; i++) {
                String key = KEY_IMAGE_NAME_PREFIX + i;
                String value = prefs.getString(key, "未知图片");
                imageNames.add(value);
            }
            Log.d(TAG, "Loaded " + imageNames.size() + " image names");
        } catch (Exception e) {
            Log.e(TAG, "Error loading image names list", e);
        }
        return imageNames;
    }
    
    /**
     * 清除所有数据
     */
    public void clearAllData() {
        try {
            SharedPreferences.Editor editor = prefs.edit();
            
            // 清除base64数据
            int base64Count = prefs.getInt(KEY_BASE64_COUNT, 0);
            for (int i = 0; i < base64Count; i++) {
                editor.remove(KEY_BASE64_PREFIX + i);
            }
            editor.remove(KEY_BASE64_COUNT);
            
            // 清除图片名称数据
            int nameCount = prefs.getInt(KEY_IMAGE_NAME_COUNT, 0);
            for (int i = 0; i < nameCount; i++) {
                editor.remove(KEY_IMAGE_NAME_PREFIX + i);
            }
            editor.remove(KEY_IMAGE_NAME_COUNT);
            
            editor.apply();
            Log.d(TAG, "Cleared all image data");
            
        } catch (Exception e) {
            Log.e(TAG, "Error clearing data", e);
        }
    }
    
    /**
     * 检查是否有数据
     */
    public boolean hasData() {
        int base64Count = prefs.getInt(KEY_BASE64_COUNT, 0);
        return base64Count > 0;
    }
    
    /**
     * 获取数据大小信息
     */
    public String getDataSizeInfo() {
        int base64Count = prefs.getInt(KEY_BASE64_COUNT, 0);
        int nameCount = prefs.getInt(KEY_IMAGE_NAME_COUNT, 0);
        return "Base64 images: " + base64Count + ", Names: " + nameCount;
    }
}
